import {
	getH264SupportFpsByBiterate,
	getH265SupportFpsByBiterate,
} from '@/pages/create/createContent/blocks/meetingParams/store.media'
import { useVirtualRoomStore } from './store'

/** 恢复媒体能力默认值 */
export function resetBitrateType() {
	const { permission, data, updateData } = useVirtualRoomStore.getState()
	let newVal = data.meetingType === 2 ? (permission.enableHDMeeting ? 6 : 4) : 3
	updateData({ _bitrateType: newVal })
	changeBitrateType(newVal)
}

export function changeBitrateType(val: number) {
	const meetingType = useVirtualRoomStore.getState().data.meetingType
	if (val !== 0) {
		if (meetingType === 2) {
			setSfuMediaValue(val)
		} else {
			setMediaAbilityVal(val)
		}
	}
}

/** 设置自定义媒体能力下拉框值*/
export const setSelfMediaAbilityVal = (val: number) => {
	const { updateData, data } = useVirtualRoomStore.getState()
	updateData({ _selfMediaValue: val })
	switch (val) {
		case 1:
			// CIF@25fps
			updateData({
				resolution: 2,
				frame: 25,
			})
			break
		case 2:
			// WCIF@25fps
			updateData({
				resolution: 14,
				frame: 25,
			})
			break
		case 3:
			// 4CIF@25fps
			updateData({
				resolution: 3,
				frame: 25,
			})
			break
		case 4:
			// W4CIF@25fps
			updateData({
				resolution: 15,
				frame: 25,
			})
			break
		case 5:
			// 720@30
			updateData({
				resolution: 12,
				frame: 30,
			})
			break
		case 6:
			// 1080@30
			updateData({
				resolution: 13,
				frame: 30,
			})
			break
		case 7:
			// 720@60
			updateData({
				resolution: 12,
				frame: 60,
			})
			break
		case 8:
			// 1080@60
			updateData({
				resolution: 13,
				frame: 60,
			})
			break
		case 9:
			//4K@30
			updateData({
				resolution: 16,
				frame: 30,
			})
			break
		case 10:
			//4K@60
			updateData({
				resolution: 16,
				frame: 60,
			})
			//当传统会议勾选h265和4K60后，默认下参纯转发
			if (data._H265 && data.meetingType === 0) {
				updateData({ sourceUsed: 2 })
			}
			break
		case 0:
			//无
			updateData({
				resolution: 0,
				frame: 0,
			})
			break
		default:
	}
}

/** 设置sfu媒体能力值 */
export const setSfuMediaValue = (val: number) => {
	const updateData = useVirtualRoomStore.getState().updateData
	updateData({ _bitrateType: val })
	if (val === 4) {
		updateData({
			frame: 30,
			rate: 2048,
			resolution: 12,
			video: 4,
		})
	} else {
		updateData({
			frame: 30,
			rate: 4096,
			resolution: 13,
			video: 4,
		})
	}
}

/** 媒体能力下拉框值改变 */
export const setMediaAbilityVal = (val: number) => {
	const { updateData, data } = useVirtualRoomStore.getState()
	updateData({ _bitrateType: val })
	let video = data._H265 ? 6 : 4
	switch (val) {
		case 0:
			updateData({
				frame: 0,
				rate: undefined,
				resolution: 0,
				video: video,
			})
			break
		case 1:
			// WCIF@25fps 192K
			updateData({
				frame: 25,
				rate: 192,
				resolution: 14,
				video: video,
			})
			break
		case 2:
			// 标清（W4CIF@25fps 256K）
			updateData({
				frame: 25,
				rate: 256,
				resolution: 15,
				video: video,
			})
			break
		case 3:
			// 高清（720P@30 2M）
			updateData({
				frame: 30,
				rate: 2048,
				resolution: 12,
				video: video,
			})
			break
		case 4:
			// 全高清（1080P@30 2M）
			updateData({
				frame: 30,
				rate: 2048,
				resolution: 13,
				video: video,
			})
			break
		case 5:
			// 全高清（1080P@60 4M）
			updateData({
				frame: 60,
				rate: 4096,
				resolution: 13,
				video: video,
			})
			break
		case 6:
			// 超高清（4K@30 4M）
			updateData({
				frame: 30,
				rate: 4096,
				resolution: 16,
				video: video,
			})
			break
		case 7:
			// 超高清（4K@60 8M）
			updateData({
				frame: 60,
				rate: 8192,
				resolution: 16,
				video: video,
			})
			//当传统会议勾选h265和4K60后，默认下参纯转发
			if (data._H265 && data.meetingType === 0) {
				updateData({ sourceUsed: 2 })
			}
			break
	}
}

/** 设置自定义媒体能力下拉框列表 */
export const setSelfMediaAbilityList = (val: number | undefined) => {
	const { updateData, data, permission } = useVirtualRoomStore.getState()
	const { _H265, meetingType, enableRtc } = data
	const isPortMeeting = meetingType === 1 && !enableRtc

	val = val ?? 0
	let list = []
	if (_H265) {
		list = getH265SupportFpsByBiterate(val, isPortMeeting)
		if (!permission.has60FpsInH265) {
			list = list.filter(function (value) {
				return !/60fps/i.test(value.label)
			})
		}
	} else {
		list = getH264SupportFpsByBiterate(val, isPortMeeting)
	}
	if (!permission['60fps']) {
		list = list.filter(function (value) {
			return !/60fps/i.test(value.label)
		})
	}
	if (!permission['4K']) {
		list = list.filter(function (value) {
			return !/4K/i.test(value.label)
		})
	}
	if (!permission.enableHDMeeting) {
		list = list.filter(function (value) {
			return !(/1080P/i.test(value.label) || /4K/i.test(value.label))
		})
	}
	//只有传统会议才能有4K@60fps
	if (meetingType !== 0) {
		list = list.filter(function (value) {
			return !/4K@60fps/i.test(value.label)
		})
	}

	updateData({ _selfMediaList: list })
}

/** 判断自定义媒体能力值是否存在于下拉列表,不存在则选项选择最顶上的选项 */
export const checkSelfMediaValueInList = () => {
	const { data, updateData } = useVirtualRoomStore.getState()
	const { _selfMediaList, _selfMediaValue } = data
	if (_selfMediaList.map((item) => item.value).indexOf(_selfMediaValue) === -1) {
		updateData({ _selfMediaValue: Number(_selfMediaList[0].value) })
	}
}
